<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$return=array();
$worker = pdo_get('jy_peiorder_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
if(!$worker['id'] || $worker['status'] !=1){
	exi('','error','/apply/');
}
$return['isopenvoice']=$this->session('isopenvoice')?1:0;
if($_RQ['op']=='main'){
	$this->model('model')->up_pass_sendlog();
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$cashdays=$this->setting['cash_day']?:1;
	$cashtime = mktime(0,0,0,date('m'),date('d')-$cashdays,date('Y'));
	$return['get_money']=pdo_getsum('jy_peiorder_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'get_money');
	$return['lastget_money']=pdo_getsum('jy_peiorder_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0),'get_money');
	$return['yesget_money']=pdo_getsum('jy_peiorder_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0,'createtime <'=>$cashtime),'get_money');
	$worker['today_money']=pdo_getsum('jy_peiorder_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$daytime),'get_money');
	$worker['today_num']=pdo_count('jy_peiorder_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$daytime));
	$worker['all_money']=pdo_getsum('jy_peiorder_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'get_money');
	$worker['all_num']=pdo_count('jy_peiorder_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$sendlog=pdo_getall('jy_peiorder_sendlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>2));
	$worker['goods_num']=0;
	$worker['allgoods_num']=0;
	foreach($sendlog as $sv){
		$sv['goods']=json_decode($sv['goods'],true);
		foreach($sv['goods'] as $gv){
			if($sv['createtime']>$daytime){
				$worker['goods_num'] +=$gv['num'];
			}
			$worker['allgoods_num'] +=$gv['num'];
		}
	}
	set_html('title','配送员中心');
	if($_SESSION['wxapp_id']){
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('17468',[1,2,3,7],'orderget','新配送任务通知','jy_peiorder');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$return['tmplIds']=$tmplIds;
	}
	$return['worker']=$worker;
	$merchs=pdo_getall('jy_merch_user',array('status'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','address','lat_lng'));
	foreach($merchs as &$mv){
		$mv['lat_lng']=json_decode($mv['lat_lng'],true);
	}
	$return['merchs']=$merchs;
	$day=date('Y-m-d',time()+24*3600);
	$params=array('day'=>$day,'sendtype'=>1,'status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$sendlogs=pdo_getall('jy_peiorder_sendlog',$params);
	$daygoods=array();
	$goodsnum=0;
	if($sendlogs){
		foreach($sendlogs as &$sv){
			$peisetgoods=json_decode($sv['goods'],true);
			foreach($peisetgoods as $k=>$gv){
				$goodsnum +=$gv['num'];
				$key=$gv['optionid']?$gv['goodsid'].'_'.$gv['optionid']:$gv['goodsid'];
				if($daygoods['goods'][$key]){
					$daygoods['goods'][$key]['num'] +=$gv['num'];
					$daygoods['goods'][$key]['lognum'] ++;
				}else{
					$gv['lognum']=1;
					$daygoods['goods'][$key]=$gv;
				}
			}
		}
	}
	$return['daylognum']=count($sendlogs);
	$return['goodsnum']=$goodsnum;
	$return['list']=$daygoods['goods'];
	$params=array('sendtype'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$params['status']=0;
	$status0=pdo_count('jy_peiorder_sendlog',$params);
	$params['status']=1;
	$status1=pdo_count('jy_peiorder_sendlog',$params);
	$params['status']=2;
	$status2=pdo_count('jy_peiorder_sendlog',$params);
	$params['status']=7;
	$status7=pdo_count('jy_peiorder_sendlog',$params);
	$params['status']=0;
	$params['day']=$day;
	$statusmday=pdo_count('jy_peiorder_sendlog',$params);
	$params['day']=date('Y-m-d');
	$params['status']=array(0,1,2);
	$statusday=pdo_count('jy_peiorder_sendlog',$params);
	$return['data']=array(
		'status0'=>$status0,
		'status1'=>$status1,
		'status2'=>$status2,
		'status7'=>$status7,
		'statusmday'=>$statusmday,
		'statusday'=>$statusday,
	);
	$return['peisetting']=$this->peisetting;
	exi($return);
	
}
if($_RQ['op']=='set'){
	if($_RQ['postdata']){
		$postdata=get_postdata();	
		$data=array(
				'headimg'=>$postdata['headimg'],
				'title'=>$postdata['title'],
				'address'=>$postdata['address'],
				'mobile'=>$postdata['mobile'],
		);
		$worker_id=pdo_getcolumn('jy_peiorder_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');		
		if($worker_id){
			pdo_update('jy_peiorder_member',$data,array('id'=>$worker_id));
			exi('修改成功','','main');		 
		}
	}
	$worker=pdo_get('jy_peiorder_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['worker']=$worker;
	exi($return);
}
if($_RQ['op']=='setonline'){
	$post=get_postdata();
	$id=$post['id'];
	$online=$post['online'];
	pdo_update('jy_peiorder_member',array('online'=>$online),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($online==0){
		exi('成功离线！');	
	}elseif($online==1){
		exi('成功上线！');	
	}elseif($online==2){
		exi('成功忙碌！');	
	}	 
}
if($_RQ['op']=='getnew'){
	$params=array(
		'status'=>2,
		'send_status'=>13,
		'uniacid'=>$_SESSION['uniacid'],
		'peisong_id'=>$_SESSION['uid'],
	);
	$this->session('isopenvoice',1);
	$order=pdo_get('jy_weishop_order',$params);
	if($order['id']){
		exi($order);
	}else{
		exi('none');
	}
}
if($_RQ['op']=='setopenvoice'){
	$isopenvoice=$_RQ['isopenvoice'];
	$isopenvoice=$isopenvoice?1:0;
	$this->session('isopenvoice',$isopenvoice);
	exi($return);
}
?>